<template>
  <el-form
    :model="ruleForm"
    status-icon
    :rules="rules"
    ref="ruleForm"
    label-width="100px"
    class="demo-ruleForm"
  >
    <el-form-item label="姓名" prop="name">
      <el-input v-model="ruleForm.name"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="pass">
      <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
    </el-form-item>
    
    <el-form-item class="submit">
      <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
    </el-form-item>
  </el-form>
</template>
<script>
import {login} from '../util/netWork';
export default {
  data() {
    var checkName = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("姓名不能为空"));
      }
      setTimeout(() => {
        if (value.length >6 ) {
            callback(new Error("姓名长度不能大于6"));
          } else {
            callback();
          }
      }, 1000);
    };
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          if(value.length>10||value.length<4){
            callback(new Error("密码长度必须大于4小于10"));
          }
        }
        callback();
      }
    };
   
    return {
      ruleForm: {
        pass: "admin",
        name: "admin"
      },
      rules: {
        pass: [{ validator: validatePass, trigger: "blur" }],
        name: [{ validator: checkName, trigger: "blur" }]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          var data={
            username:this.ruleForm.name,
            password:this.ruleForm.pass,
          }
          login(data).then(res=>{
            window.console.log(res)
            if(res.status==1){
              alert(res.msg)
            }
            if(res.status==0){
              window.sessionStorage.username=this.ruleForm.name
              this.$router.push('/')
            }
          })
        } else {
          alert("error submit!!");
        }
      });
    },
  }
};
</script>

<style lang="scss" scoped>
    .demo-ruleForm{
      max-width: 30%;
      min-width: 400px;
      margin: 150px auto;
      .submit{
        text-align: center;
      }
    }
</style>